<template>
  <div ref="lineChartRef" class="chart-container"></div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import * as echarts from 'echarts'

const lineChartRef = ref(null)
let chartInstance = null

// 初始化图表数据
const initChart = () => {
  const chart = echarts.init(lineChartRef.value)
  
  const option = {
    title: {
      text: '文章发布趋势',
      left: 'center'
    },
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: ['前端', '后端', 'AI'],
      bottom: 0
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '10%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
    },
    yAxis: {
      type: 'value',
      name: '文章数'
    },
    series: [
      {
        name: '前端',
        type: 'line',
        data: [120, 132, 101, 134, 90, 230, 210]
      },
      {
        name: '后端',
        type: 'line',
        data: [220, 182, 191, 234, 290, 330, 310]
      },
      {
        name: 'AI',
        type: 'line',
        data: [150, 232, 201, 154, 190, 330, 410]
      }
    ]
  }

  chart.setOption(option)
  chartInstance = chart
}

// 监听窗口大小变化
const handleResize = () => {
  chartInstance?.resize()
}

onMounted(() => {
  initChart()
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  chartInstance?.dispose()
  window.removeEventListener('resize', handleResize)
})
</script>

<style scoped>
.chart-container {
  width: 100%;
  height: 400px;
}
</style> 